package com.tjf.myjetpackdemo;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.tjf.myjetpackdemo.Decorator.BasePizza;
import com.tjf.myjetpackdemo.Decorator.Pizza;
import com.tjf.myjetpackdemo.Decorator.PizzaA;
import com.tjf.myjetpackdemo.Decorator.PizzaB;
import com.tjf.myjetpackdemo.RxObserverModel.Emitter;
import com.tjf.myjetpackdemo.RxObserverModel.Function;
import com.tjf.myjetpackdemo.RxObserverModel.Observable;
import com.tjf.myjetpackdemo.RxObserverModel.ObservableOnSubscribe;
import com.tjf.myjetpackdemo.RxObserverModel.Observer;
import com.tjf.myjetpackdemo.observerModel.ConcreateObservable;
import com.tjf.myjetpackdemo.observerModel.ConcreateObserve;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.tjf.myjetpackdemo", appContext.getPackageName());
    }

    @Test
    public void TestObserver() {
        ConcreateObservable observable = new ConcreateObservable();
        ConcreateObserve obs1 = new ConcreateObserve("大一");
        ConcreateObserve obs2 = new ConcreateObserve("大二");
        ConcreateObserve obs3 = new ConcreateObserve("大三");
        observable.add(obs1);
        observable.add(obs2);
        observable.add(obs3);
        observable.pushMsg("大家好");

        //链式调用
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(Emitter<String> emitter) {
                emitter.onNext("网络连接成功");
                System.out.println("上游 = " + Thread.currentThread().getName());
            }
        }).map(new Function<String, String>() {
            @Override
            public String apply(String s) {

                return s + " 数据库的验证成功";
            }
        }).map(new Function<String, String>() {
            @Override
            public String apply(String s) {

                return s + " 文件 系统验证成功";
            }
        }).subscribeOn()
                .observerOn()
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe() {
                        System.out.println("订阅成功 = " + Thread.currentThread().getName());
                    }

                    @Override
                    public void onNext(Object o) {
                        System.out.println("下游 = " + Thread.currentThread().getName());
                        System.out.println("收到的消息 = " + (String) o);
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        Pizza pizza = new BasePizza();
        PizzaA pizzaA = new PizzaA(new PizzaB(pizza));
        pizzaA.show();

    }
}